#!/usr/bin/env bash

truffleruby_post_install_hook()
{
  # Run the TruffleRuby post-install hook
  __rvm_log_command "cext" "${rvm_ruby_string} - #compiling c-extensions" \
    "$1/lib/truffle/post_install_hook.sh"
}

truffleruby_install()
{
  __rvm_setup_compile_environment "${rvm_ruby_string}" || return $?

  __rvm_cd "${rvm_src_path}"

  __rvm_fetch_ruby || return $?

  __rvm_cd "${rvm_src_path}/${rvm_ruby_string}"

  __rvm_apply_patches

  __rvm_rm_rf "${rvm_ruby_home}" && \command \mkdir -p "$(dirname "${rvm_ruby_home}")" &&
  __rvm_log_command "install" "${rvm_ruby_string} - #installing to ${rvm_ruby_home}" \
    __rvm_cp -Rf "${rvm_src_path}/${rvm_ruby_string}" "${rvm_ruby_home}" || return $?

  __rvm_cd "${rvm_ruby_home}"

  truffleruby_post_install_hook "${rvm_ruby_home}"

  # Avoid updating RubyGems for now as it seems fragile
  rvm_rubygems_version="ignore"
  __rvm_initial_gemsets_create "$rvm_ruby_home/bin/ruby"

  __rvm_post_install

  __rvm_fetch_ruby_cleanup

  rvm_log "RVM gem rubygems-bundler is not installed by default for TruffleRuby."
  rvm_log "Use 'bundle exec' instead when needed. See rvm/rvm#4765."
}
